package tech.yunland.shop.domain.websocket.interceptor;



        import java.util.Enumeration;
        import java.util.Map;

        import com.moyuer.bsf.common.web.AuthToken;
        import org.springframework.http.server.ServerHttpRequest;
        import org.springframework.http.server.ServerHttpResponse;
        import org.springframework.http.server.ServletServerHttpRequest;
        import org.springframework.web.socket.WebSocketHandler;
        import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

        import javax.servlet.http.Cookie;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;

/**
 * @author zhjh256@163.com
 * {@link} http://www.cnblogs.com/zhjh256
 */
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {

    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {

        // 解决The extension [x-webkit-deflate-frame] is not supported问题
        if (request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {
            request.getHeaders().set("Sec-WebSocket-Extensions",
                    "permessage-deflate");
        }
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
            HttpServletRequest httpservletRequest = serverRequest.getServletRequest();
            Cookie []  cookie = httpservletRequest.getCookies();
            if(cookie!=null) {
                for (int i = 0; i < cookie.length; i++) {
                    Cookie cookie1 = cookie[i];
                    if (!"name".equals(cookie1.getName())) {
                        continue;
                    }
                    attributes.put("uname", cookie1.getValue());


                }
            }
        }


        System.out.println("Before Handshake");
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request,
                               ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception ex) {
        System.out.println("After Handshake");
        super.afterHandshake(request, response, wsHandler, ex);
    }
    private HttpSession getSession(ServerHttpRequest request) {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
            HttpServletRequest servletRequest = serverRequest
                    .getServletRequest();
            Enumeration<String> parameterNames = servletRequest
                    .getParameterNames();
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                Object value = servletRequest.getAttribute(name);
            }
            // Enumeration<String> names = servletRequest.getAttributeNames();
            // while (names.hasMoreElements()) {
            // String name = names.nextElement();
            // Object value = servletRequest.getAttribute(name);
            // log.debug("name:{}, value:{}", name, value);
            // }
            return servletRequest.getSession(false);
        }
        return null;
    }
}